﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using CommunityToolkit.Mvvm.Messaging;
using DBCHM.Utils.Events;
using DBCHM.ViewModels;
using Microsoft.Extensions.DependencyInjection;

namespace DBCHM.Views;

public partial class IndexPageView : UserControl
{
    public IndexPageView()
    {
        InitializeComponent();

        this.DataContext = App.Current.Services.GetRequiredService<IndexPageViewModel>();

        // 处理消息  在首页保存通知页面信息
        WeakReferenceMessenger.Default.Register<MessageModel, string>(this, "Main",
            (r, m) =>
            {
                App.NotificationPage?.Show(new Notification(m.Title, m.Message), type: NotificationType.Information);
            });
    }

    private WindowNotificationManager _manager;

    protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
    {
        base.OnAttachedToVisualTree(e);

        // 注册通知页面
        var topLevel = TopLevel.GetTopLevel(this);
        _manager = new WindowNotificationManager(topLevel) { MaxItems = 3 };

        App.NotificationPage = _manager;
    }
}